<?php
/**
 * 毛文科
 */
namespace builder\bind;
class Form{
    protected $data = [];
    protected $action = '';
    protected $button = [];

    /**设置提交的api接口
     * @param string $action 接口
     * @return $this
     */
    public function setAction($action=''){
        $this->action = url($action);
        return $this;
    }
    /**添加设置的form对象
     * @param form对象的类型[input,checkbox,editor,file,files,img,imgs,none,password,radio,select,text,textarea,time,switch] $type
     * @param string字段名称 $name
     * @param string字段描述 $desc
     * @param string字段值 $value
     * @param array数据 $option
     * @param bool数据是否id对应值 $isIdtoName
     * @param string是否有ajaxapi $action
     * @param stringstring样式设置 $class 样式设置
     * @param string样式设置 $style 样式设置
     */
    public function setProject(string $type,string $name,string $desc,$value='',$option='',$describe='',bool $isIdtoName=false,string $action='',string $class='',string $style=''){
        if(!is_array($option)){
            $option = explode(',',$option);
        }
        if($type=='checkbox'||$type=='tree'){
            if($value){
                $value = explode(',',$value);
            }else{
                $value = [];
            }
//            echo '<pre>';
//            print_r($value);
//            print_r($option);
//            print_r($isIdtoName);
//            exit;
            if($type=='tree'){
                $value = json_encode($value,JSON_UNESCAPED_UNICODE);
                $option = json_encode($option,JSON_UNESCAPED_UNICODE);
            }
        }
        $data['type'] = $type;
        $data['name'] = $name;
        $data['desc'] = $desc;
        $data['value'] = $value;
        $data['option'] = $option;
        $data['action'] = $action;
        $data['isidtoname'] = $isIdtoName;
        $data['class'] = $class;
        $data['style'] = $style;
        $data['describe'] = $describe;
        $this->data[] = $data;
        return $this;
    }

    /**设置按钮
     * @param string $type 按钮类型[back,submit]
     * @param string $desc 描述
     * @param string $class 样式设置
     * @param string $style 样式设置
     * @return $this
     */
    public function setButton(string $type='back',string $desc='返回',string $class=null,string $style=null){
        $data['type'] = $type;
        $data['desc'] = $desc;
        $data['class'] = $class;
        $data['style'] = $style;
        $this->button[] = $data;
        return $this;
    }

    /**加载form表单
     * @return \think\response\View
     */
    public function fetch(){
//        echo '<pre>';
//        print_r($this->data);
//        print_r($this->action);
//        print_r($this->button);
//        exit;
        return view('index/form',['data'=>$this->data,'action'=>$this->action,'button'=>$this->button]);
    }
}